package com.ruoyi.review.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.manipulatorServer.domain.Manipulator;
import com.ruoyi.manipulatorServer.mapper.ManipulatorMapper;
import com.ruoyi.review.domain.ReviewContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.review.mapper.ReviewMapper;
import com.ruoyi.review.domain.Review;
import com.ruoyi.review.service.IReviewService;

/**
 * 评价Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-24
 */
@Service
public class ReviewServiceImpl implements IReviewService 
{
    @Autowired
    private ReviewMapper reviewMapper;
    @Autowired
    private ManipulatorMapper manipulatorMapper;
    /**
     * 查询评价
     * 
     * @param id 评价主键
     * @return 评价
     */
    @Override
    public Review selectReviewById(Long id)
    {
        return reviewMapper.selectReviewById(id);
    }

    /**
     * 查询评价列表
     * 
     * @param review 评价
     * @return 评价
     */
    @Override
    public List<Review> selectReviewList(Review review)
    {
        List<Review> reviews = reviewMapper.selectReviewList(review);
        for (Review review1 : reviews) {
            Long manipulatorId = review1.getManipulatorId();
            Manipulator manipulator = manipulatorMapper.selectManipulatorById(manipulatorId);
//            System.err.println(manipulator);
            review1.setManipulator(manipulator);
//            System.err.println(review1);
        }
        System.out.println(reviews);
        return reviews;
    }

    /**
     * 新增评价
     * 
     * @param review 评价
     * @return 结果
     */
    @Override
    public int insertReview(Review review)
    {
        review.setCreateTime(DateUtils.getNowDate());
//        System.err.println(review);
        review.setUpdateTime(DateUtils.getNowDate());
        IReviewContentServiceImpl iReviewContentService = new IReviewContentServiceImpl();
        List<Manipulator> list = manipulatorMapper.list();
        Long id=null;
        for (Manipulator manipulator : list) {
            if (manipulator.getNickName().equals(review.getNickName())){
                review.setNickName(manipulator.getNickName());
                id = manipulator.getId();
                break;
            }
        }
        if (id!=null){
            review.setUserId(id);
            ReviewContent reviewContent = new ReviewContent();
            reviewContent.setContent(review.getReviewContent());
            String random = String.valueOf(Math.random());
            reviewContent.setId("review:" +id.toString()+":"+random);
        }

        return reviewMapper.insertReview(review);
    }

    /**
     * 修改评价
     *
     * @param review 评价
     * @return 结果
     */
    @Override
    public int updateReview(Review review)
    {
        review.setUpdateTime(DateUtils.getNowDate());
        return reviewMapper.updateReview(review);
    }

    /**
     * 批量删除评价
     * 
     * @param ids 需要删除的评价主键
     * @return 结果
     */
    @Override
    public int deleteReviewByIds(Long[] ids)
    {
        return reviewMapper.deleteReviewByIds(ids);
    }

    /**
     * 删除评价信息
     * 
     * @param id 评价主键
     * @return 结果
     */
    @Override
    public int deleteReviewById(Long id)
    {
        return reviewMapper.deleteReviewById(id);
    }
}
